package com.maycari.captainbi.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.google.gson.JsonObject;
import com.maycari.captainbi.common.constant.CaptainBIConstant;
import com.maycari.captainbi.data.dto.Captain3Dto;
import com.maycari.captainbi.data.dto.Captain46Dto;
import com.maycari.captainbi.data.dto.Captain47Dto;
import com.maycari.captainbi.data.po.Captain17Po;
import com.maycari.captainbi.data.po.Captain46Po;
import com.maycari.captainbi.data.po.Captain47Po;
import com.maycari.captainbi.listener.SyncDto;
import com.maycari.captainbi.listener.SyncListener;
import com.maycari.captainbi.mapper.Captain46Dao;
import com.maycari.captainbi.service.Captain46Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.maycari.captainbi.util.CaptainBIUtil;
import com.maycari.tool.utils.BeanUtil;
import com.maycari.tool.utils.CollectionUtil;
import com.maycari.tool.utils.DateUtil;
import com.maycari.tool.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 郝伟
 * @since 2021-04-01
 */
@Service
@Slf4j
public class Captain46ServiceImpl extends ServiceImpl<Captain46Dao, Captain46Po> implements Captain46Service {
    @Autowired
    private JmsTemplate jmsTemplate;

    @Override
    public List<Captain46Dto> queryListAll() {
        Date start_time = DateUtil.parse("2021-04-01 00:00:00", DateUtil.PATTERN_DATETIME);
        String result = new String();
        try {
            Date end_time = DateUtil.plusMonths(start_time, 1);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
            int page = 1;
            int page_size = 20;
            String buhuo = "3";
            TreeMap<String, String> params = new TreeMap<>();
            params.put("buhuo", buhuo);
            params.put("page", String.valueOf(page));
            params.put("page_size", String.valueOf(page_size));
            params.put("start_time", simpleDateFormat.format(start_time));
            params.put("end_time", simpleDateFormat.format(end_time));
            String action = "http://cronapi.captainbi.com/index.php?m=cronapi&c=fba&a=getFbaRedundanceInventoryList";

            result = CaptainBIUtil.sendRequest(action, params);

            JSONObject jsonObject = JSON.parseObject(result);
            String data = jsonObject.getString("data");
            System.out.println(data);
            if (StringUtil.isNotBlank(data)) {
                List<Captain46Po> captain47Pos = JSON.parseArray(data, Captain46Po.class);
                for (int i = 0; i < captain47Pos.size(); i++) {
                    Captain46Po captain47Po = captain47Pos.get(i);

                    save(captain47Po);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean sync(Integer authorizationId, Date startTime, Date endTime) {
        SyncDto dto = new SyncDto();
        dto.setAuthorizationId(authorizationId);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        dto.setBeanId("captain46ServiceImpl");
        TreeMap<String, String> params = new TreeMap<>();
        params.put("start_time", simpleDateFormat.format(startTime));
        params.put("end_time", simpleDateFormat.format(endTime));
        dto.setUrl(CaptainBIConstant.CaptainBIUrl.url_46.value());
        dto.setParams(params);
        jmsTemplate.convertAndSend(SyncListener.SYNC_LISTENER, dto);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insertBatch(JSONArray jsonArray) {
        log.info("##########################################---46----批量保存------");


        for (int i = 0; i < jsonArray.size(); i++) {
            jsonArray.getJSONObject(i).put("day_sale_1", jsonArray.getJSONObject(i).get("_1_day_sale"));
            jsonArray.getJSONObject(i).put("day_sale_3", jsonArray.getJSONObject(i).get("_3_day_sale"));
            jsonArray.getJSONObject(i).put("day_sale_7", jsonArray.getJSONObject(i).get("_7_day_sale"));
            jsonArray.getJSONObject(i).put("day_sale_14", jsonArray.getJSONObject(i).get("_14_day_sale"));
            jsonArray.getJSONObject(i).put("day_sale_30", jsonArray.getJSONObject(i).get("_30_day_sale"));
            jsonArray.getJSONObject(i).put("day_sale_60", jsonArray.getJSONObject(i).get("_60_day_sale"));
            jsonArray.getJSONObject(i).put("day_sale_90", jsonArray.getJSONObject(i).get("_90_day_sale"));
        }

        List<Captain46Po> addList = new ArrayList<>();
        List<Captain46Po> updateList = new ArrayList<>();
        List<Captain46Po> captains = jsonArray.toJavaList(Captain46Po.class);

        for (Captain46Po captain : captains) {
            LambdaQueryWrapper<Captain46Po> la = new LambdaQueryWrapper<>();
            la.eq(Captain46Po::getSellerSku, captain.getSellerSku());
            if (count(la) == 0) {
                addList.add(captain);
            } else {
                LambdaUpdateWrapper laUpd = new LambdaUpdateWrapper();
                la.eq(Captain46Po::getSellerSku, captain.getSellerSku());
                update(captain,laUpd);
            }
        }

        if (CollectionUtil.isNotEmpty(addList)) {
            saveBatch(addList);
        }
        return true;
    }
}
